import VueRouter from "vue-router"

const router = new VueRouter({
    routes: [
        {
            path: '/register',
            component: () => import('@/pages/register')
        },
        {
            path: '/login',
            component: () => import('@/pages/login')
        },
        {
            path: '/admin',
            component: () => import('@/pages/admin/admin'),
            children:[
                {
                    path: 'user',
                    component: () => import('@/pages/admin/user'),
                },
                {
                    path: 'role',
                    component: () => import('@/pages/admin/role')
                },
                {
                    path: 'perm',
                    component: () => import('@/pages/admin/perm')
                },
            ]
        },
    ],
})

const VueRouterPush = VueRouter.prototype.push
const VueRouterReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function push(to){
    return VueRouterPush.call(this, to).catch(err => err)
}
VueRouter.prototype.replace = function push(to){
    return VueRouterReplace.call(this, to).catch(err => err)
}
router.beforeEach((to, from, next) => {
    next(vm => {
        if (Object.keys(to.params).length) {
          vm.$router.replace({
            name: from.name,
            params: to.params
          })
        } else {
          vm.$router.replace({
            path: from.path,
            query: to.query
          })
        }
      })
})


export default router